const db = require("./db");

function Brand(brand) {
    this.id = brand.id;
    this.brandName = brand.brandName;
    this.categoryId = brand.categoryId;
    this.brandLogo = brand.brandLogo;
    this.isDelete = brand.isDelete;
    this.hot = brand.hot;
};
Brand.addSecondCategory = (brand, callback) => {
    let selectSql = "INSERT INTO brand (id, brandName, categoryId, brandLogo, isDelete, hot) VALUES (null,?,?,?,1,?)";
    db.query(selectSql, [brand.brandName, brand.categoryId, brand.brandLogo, brand.hot], (err, result) => {
        if (err) return callback(err);
        callback(err, result);
    })
}
Brand.updateSecondCategory = (brand, callback) => {
    let selectSql = "UPDATE brand SET ";
    let params = [];
    if (brand.brandName) {
        selectSql += " brandName=? ";
        params[params.length] = brand.brandName;
    }
    // categoryId
    if (brand.categoryId && params.length == 0) {
        selectSql += " categoryId=? ";
        params[params.length] = brand.categoryId;
    } else if (brand.categoryId && params.length != 0) {
        selectSql += " ,categoryId=? ";
        params[params.length] = brand.categoryId;
    }
    // brandLogo
    if (brand.brandLogo && params.length == 0) {
        selectSql += " brandLogo=?";
        params[params.length] = brand.brandLogo;
    } else if (brand.brandLogo && params.length != 0) {
        selectSql += " ,brandLogo=? ";
        params[params.length] = brand.brandLogo;
    }
    // isDelete
    if (brand.isDelete && params.length == 0) {
        selectSql += " isDelete=?";
        params[params.length] = brand.isDelete;
    } else if (brand.isDelete && params.length == 0) {
        selectSql += " ,isDelete=? ";
        params[params.length] = brand.isDelete;
    }
    // hot
    if (brand.hot && params.length == 0) {
        selectSql += " hot=?";
        params[params.length] = brand.hot;
    } else if (brand.hot && params.length == 0) {
        selectSql += " ,hot=? ";
        params[params.length] = brand.hot;
    }

    selectSql += " WHERE id=?";
    params[params.length] = brand.id;

    db.query(selectSql, params, (err, result) => {
        if (err) return callback(err);
        callback(err, result);
    });
}

module.exports = Brand;